package com.codefactory.generator.utils;

import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelWriter {

    public static void writeToExcel(List<List<String>> data, String filePath) throws IOException {
        Workbook workbook = WorkbookFactory.create(true); // Create a new Excel workbook
        Sheet sheet = workbook.createSheet("Sheet1"); // Create a new sheet within the workbook
        // Write data to the sheet
        int rowNum = 0;
        for (List<String> rowData : data) {
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (String value : rowData) {
                Cell cell = row.createCell(colNum++);
                cell.setCellValue(value);
            }
        }

        // Write the workbook to a file
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        }

        // Close the workbook to release resources
        workbook.close();
    }

//    public static void main(String[] args) {
//        try {
//            // Example usage:
//            List<List<String>> data = new ArrayList<>();
//            data.add(Arrays.asList(new String[]{"Name", "Age", "City"}));
//            data.add(Arrays.asList(new String[]{"John", "30", "New York"}));
//            data.add(Arrays.asList(new String[]{"Alice", "25", "London"}));
//            data.add(Arrays.asList(new String[]{"Bob", "35", "Paris"}));
//            String filePath = "example.xlsx";
//            ExcelWriter.writeToExcel(data, filePath);
//            System.out.println("Excel file written successfully.");
//        } catch (IOException e) {
//            System.err.println("Error writing to Excel file: " + e.getMessage());
//        }
//    }
}
